package cn.com.customer.manager.ui.order.addOrder

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.EditText
import cn.com.customer.manager.R
import cn.com.customer.manager.adapter.GoodsAddAdapter
import cn.com.customer.manager.adapter.RecordAddAdapter
import cn.com.customer.manager.base.BaseActivity
import cn.com.customer.manager.bean.*
import cn.com.customer.manager.ui.search.SearchActivity
import com.xuexiang.xui.utils.ResUtils
import com.xuexiang.xui.utils.WidgetUtils
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog
import com.xuexiang.xui.widget.spinner.editspinner.EditSpinner
import com.xuexiang.xui.widget.spinner.editspinner.EditSpinnerAdapter
import com.xuexiang.xui.widget.spinner.materialspinner.MaterialSpinner
import kotlinx.android.synthetic.main.activity_add_order.*
import kotlinx.android.synthetic.main.layout_base_toolbar.*

class AddOrderActivity : BaseActivity(), AddOrderContract.View {
    private var mPresenter: AddOrderContract.Presenter = AddOrderPresenter(this)
    private lateinit var adapter: GoodsAddAdapter
    private lateinit var recordAdapter: RecordAddAdapter
    private var recordData = mutableListOf<BackMoneyInfo>()
    private lateinit var customerId: String
    private lateinit var customerName: String
    private var goodsList = mutableListOf<Goods>()
    private var goodsData = mutableListOf<Good1>()
    private var goodsNames = mutableListOf<String>()
    private var orderId = ""
    private lateinit var nowGood: Good1
    private var type = 0
    var params: MutableMap<String, String> = mutableMapOf()
    override fun getLayoutId(): Int = R.layout.activity_add_order

    override fun initView(savedInstanceState: Bundle?) {

        initData()
        initRv()
        ao_goods_add.setOnClickListener(this)
        ao_record_add.setOnClickListener(this)
        ao_submit.setOnClickListener(this)
        ao_customer_editSpinner.setOnClickListener(this)
        mPresenter.start()
        type = intent.getIntExtra("type", 0)
        if (type == 0) {
            customerId = intent.getStringExtra("customerId")
            customerName = intent.getStringExtra("name")
            toolbar_title.text = getString(R.string.add_order)
        } else {
            orderId = intent.getStringExtra("orderId")
            params["orderId"] = orderId
            mPresenter.getData(params)
            mPresenter.getGoodList(params)
            toolbar_title.text = getString(R.string.order_modify)
        }


//        ao_customer_editSpinner.text = customerName
    }

    private fun initRv() {
        WidgetUtils.initRecyclerView(ao_goods_rv)
        adapter = GoodsAddAdapter(R.layout.item_order_goods)
        ao_goods_rv.adapter = adapter
        adapter.setNewData(goodsList)
    }

    private fun initData() {
    }

    override fun onClick(v: View?) {
        super.onClick(v)
        when (v) {
            ao_goods_add -> {
                showCustomDialog()
            }
            ao_record_add -> {
                recordAdapter.addData(BackMoneyInfo("", "", ""))
            }
            ao_submit -> {
                addOrder()
            }
            ao_customer_editSpinner -> {
                startActivityForResult(Intent(this, SearchActivity::class.java), 1)
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK) {
            var name = data?.getStringExtra("name")
            var id = data?.getStringExtra("id")
            if (!id.isNullOrEmpty()) {
                customerId = id
            }
            if (!name.isNullOrEmpty()) {
                customerName = name
                ao_customer_editSpinner.text = name
            }
        }
    }

    fun addOrder() {
        var params = AddOrderInfo(orderId,"26", goodsList, "计算机清单", "1", "1",
                "1", "1", "1", nowPayWay.code, "1244343", "92678.98")
         if (orderId.isNullOrEmpty()){
             mPresenter.addOrder(params)
         }else{
             mPresenter.modifyOder(params)
         }
    }


    override fun addSuccess() {

    }

    override fun setStateList(data: StatusList) {


    }

    override fun setData(data: OrderDetailInfo) {
        ao_order_name_content.setText(data.orderName)
        ao_order_num_content.setText(data.orderSerialNumber)
        ao_order_count_content.setText(data.orderTotalAmount)
        ao_order_pay_way.selectedIndex = data.pay_way.toInt()-1
        ao_customer_editSpinner.text = data.name
        ao_card_name_content.setText(data.payBankName)
        ao_account_name_content.setText(data.payBankName)
        ao_card_num_content.setText(data.payBankAccount)
        ao_address_content.setText(data.payBankAddress)
        ao_code_content.setText(data.payBankCode)
    }

    override fun initGoodList(data: GoodListInfo) {
        if (!data.goods.isNullOrEmpty()){
            goodsList = data.goods as MutableList<Goods>
            adapter.notifyDataSetChanged()
        }
    }

    /**
     * 添加商品对话框
     */
    private fun showCustomDialog() {
        var dialog = MaterialDialog.Builder(this)
                .customView(R.layout.item_add_order_goods, true)
                .title(R.string.add_order)
                .positiveText(R.string.confirm_submit)
                .negativeText(R.string.picture_cancel)
                .onPositive { dialog, which ->
                    var good_num_content = dialog.findViewById<EditText>(R.id.good_num_content)
                    var good_unit_price_content = dialog.findViewById<EditText>(R.id.good_unit_price_content)
                    var good_consigneeMobile_content = dialog.findViewById<EditText>(R.id.good_consigneeMobile_content)
                    var good_consigneeName_content = dialog.findViewById<EditText>(R.id.good_consigneeName_content)
                    var good_consigneeAddress_content = dialog.findViewById<EditText>(R.id.good_consigneeAddress_content)
                    var good_maker = dialog.findViewById<EditText>(R.id.good_maker)
                    var num = good_num_content.text.toString()
                    var price = good_unit_price_content.text.toString()
                    var consigneeMobile = good_consigneeMobile_content.text.toString()
                    var consigneeName = good_consigneeName_content.text.toString()
                    var consigneeAddress = good_consigneeAddress_content.text.toString()
                    var maker = good_maker.text.toString()
                    goodsList.add(Goods(consigneeAddress, consigneeMobile, consigneeName, nowGood.id,
                            nowGood.name, num, price, maker))
                    adapter.notifyDataSetChanged()
                }
                .onNegative { dialog, which ->
                    dialog.dismiss()
                }
                .show()
        var good_name_content = dialog.findViewById<EditSpinner>(R.id.good_name_content)
        var editSpinnerAdapter = EditSpinnerAdapter(this, goodsNames)
                .setTextColor(ResUtils.getColor(R.color.light_green))
                .setTextSize(good_name_content.editText.textSize)
                .setBackgroundSelector(R.drawable.selector_edit_background)
        good_name_content.setAdapter(editSpinnerAdapter)
        good_name_content.setOnItemClickListener(object : AdapterView.OnItemClickListener {
            override fun onItemClick(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {
                nowGood = goodsData[p2]
            }
        })

    }

    lateinit var nowPayWay: Type
    override fun setPayWayList(data: PayWayResult) {
        var payWayList = mutableListOf<String>()
        if (data.payWayList.isNotEmpty()) {
            nowPayWay = data.payWayList[0]
            data.payWayList.forEach {
                payWayList.add(it.name)
            }
        }
        ao_order_pay_way.setItems(payWayList)
        ao_order_pay_way.setOnItemSelectedListener(MaterialSpinner.OnItemSelectedListener<Any?> { spinner, position, id, item ->
            nowPayWay = data.payWayList[position]
        })
        ao_order_pay_way.setOnNothingSelectedListener(MaterialSpinner.OnNothingSelectedListener { spinner -> })
    }

    override fun setGoodList(data: GoodsList) {
        goodsData = data.goods as MutableList<Good1>
        goodsData.forEach {
            goodsNames.add(it.name)
        }
    }

    override fun modifySuccess() {

    }

    override fun setPresenter(presenter: AddOrderContract.Presenter) {
        mPresenter = presenter
    }
}
